<template>
  <div>
    <div class="search-pannel">
      <el-form class="search-form" ref="searchForm" label-position="top" :model="searchData">
        <el-col :span="6">
          <el-form-item label="创建时间" prop="startTime">
            <el-date-picker v-model="searchData.startTime" type="year" value-format="yyyy"></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="地区" prop="areaId">
            <el-cascader
                ref="cascader"
                v-model="area_temp"
                :options="areaList"
                :props="areaDefaultProps"
                @change="handleChange(), (searchData.areaId = areaIds.join(','))"
            ></el-cascader>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <div class="button-list">
            <el-button type="primary" @click="onSearch">搜 索</el-button>
            <el-button @click="reset(),area_temp=''">重 置</el-button>
          </div>
        </el-col>
      </el-form>
    </div>
    <line-chart :data="series" />
  </div>
</template>

<script>
import { _getTradingVolume, _getStatisticalUser, _getStatisticalHouseAdd, _getStatisticalAppointmentAdd } from './store'
import { getAllArea } from '@/mixins'
import LineChart from '@/components/line-chart/line-chart'

export default {
  name: 'index',
  components: { LineChart },
  mixins: [getAllArea],
  data() {
    return {
      searchData: {
        startTime: '',
        areaId: ''
      },
      tradingVolume: [],
      users: [],
      houses: [],
      appointment: [],
      series: []
    }
  },
  methods: {
    async getCollection() {
      const { data, success } = await _getTradingVolume(this.searchData)
      if (!success) {
        this.$message({ type: 'error', message: '获取信息失败' })
      } else {
        this.tradingVolume = data
      }
    },
    async getStatisticalUser() {
      const { data, success } = await _getStatisticalUser(this.searchData)
      if (!success) {
        this.$message({ type: 'error', message: '获取信息失败' })
      } else {
        this.users = data
      }
    },
    async getStatisticalHouseAdd() {
      const { data, success } = await _getStatisticalHouseAdd(this.searchData)
      if (!success) {
        this.$message({ type: 'error', message: '获取信息失败' })
      } else {
        this.houses = data
      }
    },
    async getStatisticalAppointmentAdd() {
      const { data, success } = await _getStatisticalAppointmentAdd(this.searchData)
      if (!success) {
        this.$message({ type: 'error', message: '获取信息失败' })
      } else {
        this.appointment = data
      }
    },
    async onSearch() {
      await this.getCollection()
      await this.getStatisticalUser()
      await this.getStatisticalHouseAdd()
      await this.getStatisticalAppointmentAdd()
      this.series = [{
        name: '新增交易',
        type: 'line',
        stack: '总量',
        smooth: true,
        data: this.tradingVolume.map(item => item.value)
      }, {
        name: '新增用户',
        type: 'line',
        stack: '总量',
        smooth: true,
        data: this.users.map(item => item.value)
      }, {
        name: '新增房屋',
        type: 'line',
        stack: '总量',
        smooth: true,
        data: this.houses.map(item => item.value)
      }, {
        name: '新增预约',
        type: 'line',
        stack: '总量',
        smooth: true,
        data: this.appointment.map(item => item.value)
      }]
    },
    reset() {
      this.$refs.searchForm && this.$refs.searchForm.resetFields()
      this.onSearch()
    }
  },
  mounted() {
    this.onSearch()
  }
}
</script>

<style scoped>
.line-container {
  width: 100%;
  height: 700px;
}
</style>
